<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class ProductsController extends AppController {

    var $layout = 'default';
    var $uses = array(
        'Product', 'Productphoto', 'Catproduct', 'Comment'
    );
    var $components = array(
        'Security','Session'
    );
    var $scripts = array('jquery-1.7.1.js', 'jquery.lightbox-0.5.js', 'slideshowProductGalley.js', 'slideshowNews.js','common.js');

    function index($page = 1) {

        $this->set('title_for_layout', 'Sản phẩm thời trang - thời trang red');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : $page;
        $conditions = array(
            'Product.published' => 1,
        );
        $this->paginate = array(
            'fields' => array('Product.product_code,Product.id, Product.title, Product.alias, Product.thumbnail,Product.price, Product.price_promotion'),
            'conditions' => $conditions,
            'limit' => 16,
            'order' => array(
                'Product.id' => 'DESC'
            ),
        );


        $data = $this->paginate('Product');
        $this->set('total', $this->params['paging']['Product']['count']);
        $this->set('data', $data);
        $this->set('product_image_thumb_url_path', Configure::read('product_image_thumb_url_path'));
        $this->set('start_page', $page);
    }
    function promotion($page = 1) {

        $this->set('title_for_layout', 'Sản phẩm khuyến mãi - thời trang red');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : $page;
        $conditions = array(
            'Product.published' => 1,
            'Product.is_promotion' => 1,
        );
        $this->paginate = array(
            'fields' => array('Product.product_code,Product.id, Product.title, Product.alias, Product.thumbnail,Product.price, Product.price_promotion'),
            'conditions' => $conditions,
            'limit' => 16,
            'order' => array(
                'Product.price_promotion' => 'ASC'
            ),
        );


        $data = $this->paginate('Product');
        $this->set('total', $this->params['paging']['Product']['count']);
        $this->set('data', $data);
        $this->set('product_image_thumb_url_path', Configure::read('product_image_thumb_url_path'));
        $this->set('start_page', $page);
    }
    function cat_product($id = null, $slug = null) {
        $this->set('title_for_layout', 'Sản phẩm thời trang - thời trang red');

        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;

        $list_ids = array();
        $this->Catproduct->getChildAllById($id, $list_ids);
        if (is_array($list_ids)) {
            $list_ids[] = $id;
        } else {
            $list_ids = array($id);
        }
        $conditions = array(
            'Product.published' => 1,
            'Product.cat_id IN (' . implode(',', $list_ids) . ')'
        );
        $this->paginate = array(
            'fields' => array('Product.product_code,Product.id, Product.title, Product.alias, Product.thumbnail,Product.price, Product.price_promotion'),
            'conditions' => $conditions,
            'limit' => 16,
            'order' => array(
                'Product.id' => 'DESC'
            ),
        );
        
        $data = $this->paginate('Product');
        $this->set('total', $this->params['paging']['Product']['count']);
        $this->set('data', $data);
        $this->set('product_image_thumb_url_path', Configure::read('product_image_thumb_url_path'));
        $this->set('start_page', $page);
    }

    function detail($id) {
        $content = $this->Product->read(array('*'), $id);
        
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $conditions = array(
            'Comment.published' => 1,
            'Comment.module' => 'products',
            'Comment.module_unique_id' => $id,
        );
        $this->paginate = array(
            'fields' => array("DATE_FORMAT(Comment.created,'%d-%m-%Y %h:%h %p') AS 'Comment.created', Comment.id, Comment.subject, Comment.content, User.avatar, User.id, User.name"),
            'conditions' => $conditions,
            'limit' => 10,
            'order' => array(
                'Comment.id' => 'DESC'
            ),
        );
        $data_comments = $this->paginate('Comment');
        $this->set('total', $this->params['paging']['Comment']['count']);
        $this->set('data_comments', $data_comments);
        
        $product_history = $this->Session->read('product_history');
        $product_history = is_array($product_history) ? $product_history : array();
        if (sizeof($product_history) <= 0) {
            $product_history[] = $content;
        } else {
            array_unshift($product_history, $content);
        }
        if (sizeof($product_history) > Configure::read('max_product_history')) {
            array_pop($product_history);
        }
        $this->Session->write('product_history', $product_history);

        $this->Product->id = $id;
        $this->Product->saveField('view_times', $content['Product']['view_times'] + 1);

        //get gallery
        $product_photos = $this->Productphoto->find('all', array(
                    'conditions' => array('Productphoto.product_id' => $id),
                    'fields' => 'Productphoto.image_small, Productphoto.folder_url, Productphoto.folder_path, Productphoto.images',
                    'order' => array(
                        'Productphoto.id' => 'DESC'
                    ),
                ));

        $this->set('content', $content);
        $this->set('product_photos', $product_photos);
        $this->set('title_for_layout', "Chi tiết &gt;&gt;{$content['Product']['title']}- Thời trang RED");
        $this->set('request_id', $id);
        $this->set('product_gallery_thumb_url_path', Configure::read('product_gallery_thumb_url_path'));
    }

    function more_information($id) {
        $content = $this->Product->read(array('*'), $id);

        $this->set('content', $content);
        $this->set('title_for_layout', "Chi tiết &gt;&gt;{$content['Product']['title']}- Thời trang RED");
    }

    function product_size() {
        $this->set('title_for_layout', "Các mẫu size có sẵn - Thời trang RED");
    }

}

?>
